package itcast.dao.impl;

import itcast.dao.ScoredromDao;
import itcast.domain.Build;
import itcast.domain.Score;
import itcast.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class ScoredromDaoImpl implements ScoredromDao {
    /**
     * 申明JdbcTemplate类,出入连接池
     */
    private JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());

    //数据库中查找学生信息

    @Override
    public List<Score> findscore_dromAll() {
        String sql="select score_id,drom_id,build_id,build_name,drom_name,drom_type,score_fraction,score_remark,score_date from score,build,drom where score.score_dromid=drom.drom_id and drom.drom_buildid=build.build_id";
        //query返回list集合，BeanPropertyRowMapper指定字节码
        List<Score> scores=template.query(sql,new BeanPropertyRowMapper<Score>(Score.class));

        return scores;
    }
       //王数据库插入评分信息

    @Override
    public void insertscore(Score score) {
      /* String sql1="update student,drom set student_domitoryid=1,drom_id=1 where student.student_domitoryid=drom.drom_id and drom.drom_buildid=build.build_id and drom_name=? and build_id=?";
        String sql="insert into score(score_dromid,score_fraction,score_remark,score_date) values(1,?,?,?)";
        template.update(sql1,score.getDrom_name());
        template.update(sql,score.getScore_fraction(),score.getScore_remark(),score.getScore_date());
        */
      String sql1="select drom_id from drom, build where drom_buildid=build_id and drom_name=? and build_name=?";
      int drom_id=template.queryForObject(sql1,Integer.class,score.getDrom_name(),score.getBuild_name());
      String sql2="insert into score (score_dromid,score_fraction,score_remark,score_date) values(?,?,?,?)";
      template.update(sql2,drom_id,score.getScore_fraction(),score.getScore_remark(),score.getScore_date());
      }

    //根据id删除数据

    @Override
    public void delete_scoredrom(int score_id) {
        String sql="delete from score where score_id=? ";
        template.update(sql,score_id);
    }
}
